import java.util.ArrayList;
import java.util.Stack;


/**
 *
 * @author matheus
 */
public class GUIJogoDaVelha extends javax.swing.JFrame {

	private Stack<Integer> jogadas;
	private ArrayList<Jogador> jogadores;

    public GUIJogoDaVelha(Stack<Integer> jogadas, Jogador j1, Jogador j2) {
        initComponents();
        this.jogadas = jogadas;
        jogadores = new ArrayList<Jogador>();
        jogadores.add(j1);
        jogadores.add(j2);
        new TrocadorDeImagens(jLabel1).start(); //COmeca a Thread que fica trocando de imagens
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        umXum = new javax.swing.JButton();
        umXdois = new javax.swing.JButton();
        umXtres = new javax.swing.JButton();
        doisXum = new javax.swing.JButton();
        doisXdois = new javax.swing.JButton();
        doisXtres = new javax.swing.JButton();
        tresXum = new javax.swing.JButton();
        tresXdois = new javax.swing.JButton();
        tresXtres = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        umXum.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                umXumActionPerformed(evt);
            }
        });

        umXdois.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                umXdoisActionPerformed(evt);
            }
        });

        umXtres.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                umXtresActionPerformed(evt);
            }
        });

        doisXum.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                doisXumActionPerformed(evt);
            }
        });

        doisXdois.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                doisXdoisActionPerformed(evt);
            }
        });

        doisXtres.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                doisXtresActionPerformed(evt);
            }
        });

        tresXum.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tresXumActionPerformed(evt);
            }
        });

        tresXdois.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tresXdoisActionPerformed(evt);
            }
        });

        tresXtres.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tresXtresActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(37, 37, 37)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(doisXum, javax.swing.GroupLayout.PREFERRED_SIZE, 109, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(doisXdois, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(doisXtres, javax.swing.GroupLayout.PREFERRED_SIZE, 109, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(umXum, javax.swing.GroupLayout.PREFERRED_SIZE, 109, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(umXdois, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(umXtres, javax.swing.GroupLayout.PREFERRED_SIZE, 109, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(tresXum, javax.swing.GroupLayout.PREFERRED_SIZE, 109, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(tresXdois, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(tresXtres, javax.swing.GroupLayout.PREFERRED_SIZE, 109, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 52, Short.MAX_VALUE)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 416, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(tresXdois, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(layout.createSequentialGroup()
                                .addContainerGap()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(umXum, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(umXdois, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(umXtres, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(doisXum, javax.swing.GroupLayout.PREFERRED_SIZE, 76, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(doisXdois, javax.swing.GroupLayout.PREFERRED_SIZE, 76, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(doisXtres, javax.swing.GroupLayout.PREFERRED_SIZE, 76, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(tresXum, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(tresXtres, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE))))
                        .addGap(0, 123, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    //Os enventos foram tornados publicos para poderem ser chamados a partir da funcao principal visando a reutilizacao do codigo
    
    public void umXumActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_umXumActionPerformed
    	umXum.setText(jogadores.get(jogadas.peek()).getMarca());
    	umXum.setEnabled(false);
    	if(jogadas.peek() == 1){ //Se foi chamado a partir de um clique em botao, sincroniza a partir da pilha de jogadas
    		
    		synchronized(jogadas)
    		{
    			//Coloca os valores correspontes
    			jogadas.push(1);
    			jogadas.push(1);
    			jogadas.notify(); //Alguem que esteja esperando
    		}
    	}


    }//GEN-LAST:event_umXumActionPerformed

    public void umXdoisActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_umXdoisActionPerformed
    	umXdois.setText(jogadores.get(jogadas.peek()).getMarca());
    	umXdois.setEnabled(false);
    	
    	if(jogadas.peek() == 1){
    		synchronized(jogadas)
    		{
    			jogadas.push(2);
    			jogadas.push(1);
    			jogadas.notify();

    		}
    	}

	}//GEN-LAST:event_umXdoisActionPerformed

    public void umXtresActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_umXtresActionPerformed
    	umXtres.setText(jogadores.get(jogadas.peek()).getMarca());
    	umXtres.setEnabled(false);

    	if(jogadas.peek() == 1 ){
    		synchronized(jogadas)
    		{
    			jogadas.push(3);
    			jogadas.push(1);
    			jogadas.notify();

    		}
    	}

    }//GEN-LAST:event_umXtresActionPerformed

    public void doisXumActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_doisXumActionPerformed
    	doisXum.setText(jogadores.get(jogadas.peek()).getMarca());
    	doisXum.setEnabled(false);
    	if(jogadas.peek() == 1){
    		synchronized(jogadas)
    		{
    			jogadas.push(1);
    			jogadas.push(2);
    			jogadas.notify();

    		}
    	}

    }//GEN-LAST:event_doisXumActionPerformed

    public void doisXdoisActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_doisXdoisActionPerformed
    	doisXdois.setText(jogadores.get(jogadas.peek()).getMarca());
    	doisXdois.setEnabled(false);


    	if(jogadas.peek() == 1){
    		synchronized(jogadas)
    		{
    			jogadas.push(2);
    			jogadas.push(2);
    			jogadas.notify();

    		}
    	}

    }//GEN-LAST:event_doisXdoisActionPerformed

    public void doisXtresActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_doisXtresActionPerformed
    	doisXtres.setText(jogadores.get(jogadas.peek()).getMarca());
    	doisXtres.setEnabled(false);

    	if(jogadas.peek() == 1){
    		synchronized(jogadas)
    		{
    			jogadas.push(3);
    			jogadas.push(2);
    			jogadas.notify();

    		}
    	}


    }//GEN-LAST:event_doisXtresActionPerformed

    public void tresXumActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tresXumActionPerformed
    	tresXum.setText(jogadores.get(jogadas.peek()).getMarca());
    	tresXum.setEnabled(false);
    	
    	if(jogadas.peek() == 1){
    		synchronized (jogadas) {
    			jogadas.push(1);
    			jogadas.push(3);
    			jogadas.notify();
    		}
    	}


    }//GEN-LAST:event_tresXumActionPerformed

    public void tresXdoisActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tresXdoisActionPerformed

    	tresXdois.setText(jogadores.get(jogadas.peek()).getMarca());
    	tresXdois.setEnabled(false);

    	if(jogadas.peek() == 1){
    		synchronized(jogadas)
    		{
    			jogadas.push(2);
    			jogadas.push(3);
    			jogadas.notify();

    		}
    	}

    }//GEN-LAST:event_tresXdoisActionPerformed

    public void tresXtresActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tresXtresActionPerformed
    	tresXtres.setText(jogadores.get(jogadas.peek()).getMarca());
    	tresXtres.setEnabled(false);
    	if(jogadas.peek() == 1){
    		synchronized(jogadas)
    		{
    			jogadas.push(3);
    			jogadas.push(3);
    			jogadas.notify();

    		}
    	}

    }//GEN-LAST:event_tresXtresActionPerformed

    //Retorna o jogador atual para a interface
    public String getUltimaJogada()
    {
    	int i = jogadas.peek();
    	if(i == 1)
    		return jogadores.get(0).getMarca();
    	else
    		return jogadores.get(1).getMarca();
    	
    }
    
    
    private javax.swing.JButton doisXdois;
    private javax.swing.JButton doisXtres;
    private javax.swing.JButton doisXum;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JButton tresXdois;
    private javax.swing.JButton tresXtres;
    private javax.swing.JButton tresXum;
    private javax.swing.JButton umXdois;
    private javax.swing.JButton umXtres;
    private javax.swing.JButton umXum;

}
